package com.hxz.graduationdesign.service.impl;

import com.hxz.graduationdesign.mapper.VideoMapper;
import com.hxz.graduationdesign.pojo.dto.UpVideoDto;
import com.hxz.graduationdesign.pojo.entity.UpVideo;
import com.hxz.graduationdesign.pojo.entity.Video;
import com.hxz.graduationdesign.pojo.vo.UpVideoVo;
import com.hxz.graduationdesign.pojo.vo.VideoVo;
import com.hxz.graduationdesign.service.IVideoService;
import com.hxz.graduationdesign.util.BilibiliVideoDataFetcher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.hxz.graduationdesign.util.BilibiliUpVideoCrawler.getUpVideo;

@Service
@Slf4j
public class VideoService implements IVideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Override
    public VideoVo queryVideo(String bv) {
        VideoVo videoVo = videoMapper.selectVideoByBv(bv);
        if (videoVo!=null){

            if (videoVo.getUsername() == null) {
                log.debug("补全视频信息："+videoVo);

                int i = updateVideo(bv);
                if(i>0){

                    return videoMapper.selectVideoByBv(bv);

                }else {

                    return null;

                }

            }


            log.debug("查询视频:"+videoVo);
            return videoVo;
        }
        addVideo(bv);
        return videoMapper.selectVideoByBv(bv);

    }

    @Override
    public int addVideo(String bv) {
        Video video = BilibiliVideoDataFetcher.getVideoData(bv);
        log.debug("添加视频:"+video);
        if(video.getTitle()!=null){
            return videoMapper.insert(video);
        }

        throw new RuntimeException("找不到bv对应视频，视频不存在或视频已下架");
    }

    @Override
    public int updateVideo(String bv) {
        Video video = BilibiliVideoDataFetcher.getVideoData(bv);
        return videoMapper.updateVideo(video);
    }

    @Override
    public List<UpVideoVo> queryUpVideo(String uid) {

        List<UpVideoVo> upVideoVos = videoMapper.selectUpVideo(uid);
        if(upVideoVos.isEmpty()){

            try {
                List<UpVideoDto> upVideoDtos = getUpVideo(uid);

                List<UpVideo> upVideos = new ArrayList<>();
                for (UpVideoDto upVideoDto : upVideoDtos) {
                    UpVideo upVideo = new UpVideo();
                    BeanUtils.copyProperties(upVideoDto, upVideo);
                    upVideos.add(upVideo);
                }

                int i = videoMapper.insertUpVideo(upVideos);

                if (i>0){
                    return videoMapper.selectUpVideo(uid);
                }else {
                    return null;
                }

            } catch (Exception e) {
                throw new RuntimeException("获取视频失败，uid错误或该用户并未上传视频");
//                throw new RuntimeException(e);
            }



        }else {
            return upVideoVos;
        }

    }

    @Override
    public List<UpVideoVo> updateUpVideo(String uid) {
        videoMapper.deleteByUid(uid);
        return queryUpVideo(uid);
    }

    @Override
    public VideoVo updateSingleVideo(String bv) {

        videoMapper.deleteByBv(bv);
        return queryVideo(bv);

    }


}
